package edu.uoc.tds.pec4.mantenimiento.view;

/**
*
* @author jfdelrio
*/

import java.rmi.RemoteException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import edu.uoc.tds.pec4.beans.Curso;
import edu.uoc.tds.pec4.client.PanelMenusUI;
import edu.uoc.tds.pec4.i18n.TDSLanguageUtils;
import edu.uoc.tds.pec4.mantenimiento.controller.ComboItem;
import edu.uoc.tds.pec4.mantenimiento.controller.ControladorCurso;


public class ListadoCursos extends javax.swing.JFrame {
	
	private static final long serialVersionUID = 1L;
	private JButton jButton1;
    private JButton jButton2;
    private JButton jButton3;
    private JButton jButton4;
    private JButton jButton5;
    private JButton jButton6;
    private JCheckBox jCheckBox1;
    private JCheckBox jCheckBox3;
    private JCheckBox jCheckBox6;
    private JCheckBox jCheckBox7;
    private JComboBox jComboBox1;
    private JLabel jLabel1;
    private JLabel jLabel10;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JLabel jLabel5;
    private JLabel jLabel6;
    private JLabel jLabel7;
    private JLabel jLabel9;
    private JPanel jPanel1;
    private JPanel jPanel2;
    private JScrollPane jScrollPane1;
    private JTable jTable1;
    private JTextField jTextField2;
    private JTextField jTextField3;
    private JTextField jTextField4;
    private JTextField jTextField6;
    private JTextField jTextField8;
    private JTextField jTextField9;	

    public ListadoCursos() {
        initComponents();
    }

    private void initComponents() {

        jScrollPane1 = new JScrollPane();
        jTable1 = new JTable();
        jLabel1 = new JLabel();
        jButton1 = new JButton();
        jButton2 = new JButton();
        jButton3 = new JButton();
        jButton4 = new JButton();
        jPanel1 = new JPanel();
        jLabel3 = new JLabel();
        jLabel4 = new JLabel();
        jLabel5 = new JLabel();
        jTextField2 = new JTextField();
        jTextField3 = new JTextField();
        jTextField4 = new JTextField();
        jLabel6 = new JLabel();
        jLabel7 = new JLabel();
        jTextField6 = new JTextField();
        jComboBox1 = new JComboBox();
        jTextField8 = new JTextField();
        jLabel9 = new JLabel();
        jTextField9 = new JTextField();
        jLabel10 = new JLabel();
        jCheckBox1 = new JCheckBox();
        jPanel2 = new JPanel();
        jCheckBox3 = new JCheckBox();
        jCheckBox6 = new JCheckBox();
        jCheckBox7 = new JCheckBox();
        jButton5 = new JButton();
        jButton6 = new JButton();

        jButton3.setEnabled(false);
        
        InputMap im = (InputMap)UIManager.get("Button.focusInputMap");
        im.put( KeyStroke.getKeyStroke( "ENTER" ), "pressed" );
        im.put( KeyStroke.getKeyStroke( "released ENTER" ), "released" );        
        getRootPane().setDefaultButton(jButton5);
        
		String locale = PanelMenusUI.getIdioma();
		boolean existeFicheroMensajes = TDSLanguageUtils.setDefaultLanguage("i18n/"+locale+"_messages");
        if (!existeFicheroMensajes) {
                JOptionPane.showMessageDialog(null, "ERROR: no se encuentra el fichero 'i18n/message.properties'.\nCorrija el error y vuelva a intentarlo","Error",0);
                System.exit(-1); 
        }        
        
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(TDSLanguageUtils.getMessage("mantenimiento.cursos.titulo")); 
        setResizable(false); 

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
        	new Object [][] {},
            new String [] {
        		TDSLanguageUtils.getMessage("general.codigo"), 
        		TDSLanguageUtils.getMessage("general.nombre"), 
        		TDSLanguageUtils.getMessage("mantenimiento.cursos.contenido"), 
        		TDSLanguageUtils.getMessage("mantenimiento.cursos.evaluacion"), 
        		TDSLanguageUtils.getMessage("mantenimiento.cursos.percetmin"), 
        		TDSLanguageUtils.getMessage("mantenimiento.cursos.descripcion"), 
        		TDSLanguageUtils.getMessage("mantenimiento.cursos.objetivos"), 
        		TDSLanguageUtils.getMessage("mantenimiento.cursos.materiales"), 
        		TDSLanguageUtils.getMessage("mantenimiento.cursos.modelo"), 
        		TDSLanguageUtils.getMessage("mantenimiento.cursos.plandocente"), 
        		TDSLanguageUtils.getMessage("mantenimiento.cursos.bibliografia"), 
        		TDSLanguageUtils.getMessage("general.fechaAlta"), 
        		TDSLanguageUtils.getMessage("general.fechaBaja")
            }
        ) {

			private static final long serialVersionUID = 1L;
			boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        
        jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jTable1.getSelectionModel().addListSelectionListener(
            new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent event) {                    	
                	int numRegistro = jTable1.getSelectedRow();   
                	if (numRegistro >= 0) {
                    	String fechaBaja = (String) jTable1.getValueAt(numRegistro, 12);
                    	if (fechaBaja == null) {
                    		jButton3.setEnabled(false);
                    	} else {
                    		jButton3.setEnabled(true);
                    	}
                	}
                }
            }
        ); 
        
        jScrollPane1.setViewportView(jTable1);           
        
        /* BOTONERA */ 
        jLabel1.setText(TDSLanguageUtils.getMessage("mantenimiento.pantallaListado.labelBotonera"));
        jButton1.setText(TDSLanguageUtils.getMessage("mantenimiento.pantallaListado.botonAgnadir"));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                altaAction(evt);
            }
        });

        jButton2.setText(TDSLanguageUtils.getMessage("mantenimiento.pantallaListado.botonModificar"));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modificarAction(evt);
            }
        });
        
        jButton3.setText(TDSLanguageUtils.getMessage("mantenimiento.pantallaListado.botonDevolverVigencia"));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
					devolverVigenciaAction(evt);
				} catch (RemoteException e) {
					JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("cliente.error.conectar"), TDSLanguageUtils.getMessage("cliente.error.titulo"), 0);
					e.printStackTrace();
				} catch (SQLException e) {
					JOptionPane.showMessageDialog(null, e.getMessage(), TDSLanguageUtils.getMessage("baseDatos.error.titulo"), JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });

        jButton4.setText(TDSLanguageUtils.getMessage("mantenimiento.pantallaListado.botonBaja"));
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bajaAction(evt);
            }
        }); 

        
        /* FILTROS */ 
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(TDSLanguageUtils.getMessage("mantenimiento.pantallaListado.labelFiltros")));
        jLabel3.setText(TDSLanguageUtils.getMessage("general.nombre") + ":");
        jLabel4.setText(TDSLanguageUtils.getMessage("mantenimiento.cursos.descripcion") + ":");
        jLabel5.setText(TDSLanguageUtils.getMessage("mantenimiento.cursos.materiales") + ":");
        jLabel6.setText(TDSLanguageUtils.getMessage("mantenimiento.cursos.modelo") + ":");
        jLabel7.setText(TDSLanguageUtils.getMessage("mantenimiento.cursos.evaluacion") + ":");
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(ControladorCurso.getTiposEvaluacion()));
        jLabel9.setText(TDSLanguageUtils.getMessage("general.fechaAlta") + ":");
        jLabel10.setText(TDSLanguageUtils.getMessage("general.fechaBaja") + ":");
        jCheckBox1.setText(TDSLanguageUtils.getMessage("mantenimiento.pantallaListado.noVigentes"));
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	toogleCampoFechaBaja(evt);
            }
        });
        
        /* LAYOUT */
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                                        .addComponent(jTextField3)
                                        .addComponent(jTextField2))
                                    .addComponent(jTextField6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jCheckBox1)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBox1)
                .addGap(7, 7, 7)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        /* FILTROS 2 */        
        jPanel2.setBorder(BorderFactory.createTitledBorder(TDSLanguageUtils.getMessage("mantenimiento.filtrosordenacion ")));        
        jCheckBox3.setText(TDSLanguageUtils.getMessage("general.nombre"));
        jCheckBox6.setText(TDSLanguageUtils.getMessage("general.fechaAlta"));
        jCheckBox7.setText(TDSLanguageUtils.getMessage("general.fechaBaja") + TDSLanguageUtils.getMessage("mantenimiento.pantallaListado.soloNoVigentes"));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox3)
                    .addComponent(jCheckBox6)
                    .addComponent(jCheckBox7))
                .addContainerGap(352, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jCheckBox3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox7))
        );


        jButton5.setText(TDSLanguageUtils.getMessage("mantenimiento.cursos.buscarCursos"));
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarUsuarios(evt);
            }
        });

        jButton6.setText(TDSLanguageUtils.getMessage("general.salir"));
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	salirAction(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(76, 76, 76)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 177, Short.MAX_VALUE)
                        .addComponent(jButton3)
                        .addGap(77, 77, 77)
                        .addComponent(jButton4))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 0, Short.MAX_VALUE))))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton6)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 110, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jButton6))))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
        pintarTabla();
    }

    
    
    /* METODOS */  
    public void pintarTabla() {
    	((DefaultTableModel) jTable1.getModel()).setRowCount(0);
		try {
			for(String[] e: ControladorCurso.getlCursos("")){
				((DefaultTableModel) jTable1.getModel()).addRow(e);
			}
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), TDSLanguageUtils.getMessage("baseDatos.error.titulo"), JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		jTable1.repaint();    	
    }  
    
    public void pintarTabla(String condicion) {    	
    	((DefaultTableModel) jTable1.getModel()).setRowCount(0);
		try {
			for(String[] e: ControladorCurso.getlCursos(condicion)){
				((DefaultTableModel) jTable1.getModel()).addRow(e);
			}
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), TDSLanguageUtils.getMessage("baseDatos.error.titulo"), JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		jTable1.repaint();    	
    }      
    
    public void buscarUsuarios(java.awt.event.ActionEvent evt) {    	
    	boolean hayOrden = false;
    	String consulta = " WHERE 1=1";   

    	if (jTextField2.getText().length()>0) {
    		consulta = consulta + " AND nombre ilike '%" + jTextField2.getText() + "%'";
    	}    	
    	if (jTextField3.getText().length()>0) {    		
    		consulta = consulta + " AND descripcion ilike '%" + jTextField3.getText() + "%'";
    	}
    	if (jTextField4.getText().length()>0) {    		
    		consulta = consulta + " AND materiales ilike '%" + jTextField4.getText() + "%'";
    	}    	
    	if (jTextField6.getText().length()>0) {    		
    		consulta = consulta + " AND modelo ilike '%" + jTextField6.getText() + "%'";
    	}    	
    	if (jTextField8.getText().length()>0) {    		
    		consulta = consulta + " AND f_alta ='" + deStringToDate(jTextField8.getText()) + "'";
    	}    	
    	if (!jCheckBox1.isSelected() && jTextField9.getText().length()>0) {
    		consulta = consulta + " AND f_baja ='" + deStringToDate(jTextField9.getText()) + "'";
    	}    	
    	ComboItem tipo = (ComboItem) jComboBox1.getSelectedItem();
    	int idTipo = Integer.parseInt(tipo.getValue());
    	if (idTipo > 0) {
    		Curso curso = new Curso();
    		curso.setEvaluacionFinalStr2(tipo.toString());
    		consulta = consulta + " AND evaluacion ='" + curso.getEvaluacionFinalStr() + "'";
    	}
    	if (jCheckBox1.isSelected()) {
    		consulta = consulta + " AND f_baja IS NOT NULL";
    	}      

    	
    	if (jCheckBox3.isSelected() || jCheckBox6.isSelected() || jCheckBox7.isSelected()) { 
    		hayOrden = true;
    		consulta = consulta + " ORDER BY";    		
    	}
    	if (jCheckBox3.isSelected()) {
    		consulta = consulta + " nombre,";  
    	}         	
    	if (jCheckBox6.isSelected()) {
    		consulta = consulta + " f_alta,";  
    	}     	
    	if (jCheckBox7.isSelected()) {
    		consulta = consulta + " f_baja,";  
    	} 
		if (hayOrden) {
    		consulta = consulta.substring(0, consulta.length() - 1);
    	}
    	   	
    	pintarTabla(consulta);
    }         
  
    private void altaAction(java.awt.event.ActionEvent evt) {
    	new CrudCurso(this, "alta").setVisible(true);    	
    }
    
    private void modificarAction(java.awt.event.ActionEvent evt) {    
    	int numRegistro = jTable1.getSelectedRow();
    	String idCurso;
    	if(numRegistro != -1){
    		idCurso = (String) jTable1.getValueAt(numRegistro, 0);
    		new CrudCurso(idCurso, this, "modificacion").setVisible(true);  
    	} else {
    		JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("mantenimiento.cursos.selecc.modi"));
    	}    	    	  	
    }     
 
    private void devolverVigenciaAction(java.awt.event.ActionEvent evt) throws RemoteException, SQLException, Exception {
    	if(JOptionPane.showConfirmDialog (null, TDSLanguageUtils.getMessage("mantenimiento.pantallaListado.message.vigencia"),
    			TDSLanguageUtils.getMessage("mantenimiento.cursos.titulo"), JOptionPane.OK_CANCEL_OPTION)==JOptionPane.OK_OPTION) {    		
    		int numRegistro = jTable1.getSelectedRow();
    		int idCurso = Integer.parseInt((String) jTable1.getValueAt(numRegistro, 0));
			ControladorCurso.devolverVigenciaCurso(idCurso);  
			pintarTabla();
		}    
    }    
    
    private void bajaAction(java.awt.event.ActionEvent evt) {
    	int numRegistro = jTable1.getSelectedRow();
    	String idCurso;
    	if(numRegistro != -1){
    		idCurso = (String) jTable1.getValueAt(numRegistro, 0);
    		new CrudCurso(idCurso, this, "baja").setVisible(true);  
    	} else {
    		JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("mantenimiento.cursos.selecc.baja"));
    	}   
    } 
    
    private void toogleCampoFechaBaja(java.awt.event.ActionEvent evt) {
    	if(jCheckBox1.isSelected()) {
    		jTextField9.setEditable(false);
    	} else {
    		jTextField9.setEditable(true);
    	}    	 
    }   
    
    private void salirAction(java.awt.event.ActionEvent evt) {
    	setVisible(false); 
    	dispose();
    }   
    
    public static synchronized java.util.Date deStringToDate(String fecha) {
    	fecha = fecha.replace("/", "-");        
        Date fechaEnviar = null;
        try {
        	Date date = new SimpleDateFormat("dd-MM-yyyy").parse(fecha);
        	fecha = new SimpleDateFormat("yyyy-MM-dd").format(date);        	
            fechaEnviar = new SimpleDateFormat("yyyy-MM-dd").parse(fecha);
            return fechaEnviar;
        } catch (ParseException ex) {
			JOptionPane.showMessageDialog(null, ex.getMessage(), "error fecha", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    }    

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ListadoCursos().setVisible(true);
            }
        });
    }

}
